<template>
	<basic-container>
		<avue-crud :option="option" :data="data" ref="formRef" v-model="form" :table-loading="loading" :permission="permissionList"
		 :page="page" @refresh-change="refreshChange" @search-change="searchChange" @search-reset="searchReset"
		 @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
			<!-- 新增按钮  -->
			<template slot="menuLeft">
				<el-button type="primary" size="small" @click="addCoupon">新增优惠券</el-button>
			</template>
			<template slot-scope="{row}" slot="couponStatus">
				<el-switch v-if="row.couponStatus !=  3" @change="rowSwitchChange(row)" v-model="row.couponStatus" active-value="1"
				 inactive-value="2" active-color="#13ce66" inactive-color="#ff4949">
				</el-switch>
				<div v-else>已经过期</div>
			</template>
			<template slot-scope="scope" slot="menu">
				<el-button icon="el-icon-edit" size="small" type="text" @click="grantShow(scope.row)">参加优惠</el-button>
				<!--				<el-button icon="el-icon-edit" size="small" type="text" @click="editShow(scope.row)">停用</el-button>-->
				<el-button icon="el-icon-view" size="small" type="text" @click="goodsDetailShow(scope.row)">活动商品</el-button>
				<el-button icon="el-icon-view" size="small" type="text" @click="detailShow(scope.row)">查看使用详情</el-button>
			</template>
			<template slot-scope="scope" slot="dateScope">
				<el-tag color="#fff" style="border: none;color: #333333;">{{scope.row.couponBeginTime}}~{{scope.row.couponEndTime}}</el-tag>
			</template>
			<!--<template slot="search">
				<el-col>
					<el-form-item label="使用时间">
						<el-date-picker placeholder="开始时间" value-format="yyyy-MM-dd" v-model="couponBeginTime" type="date">
						</el-date-picker>
					</el-form-item>
					<el-form-item label="至">
						<el-date-picker placeholder="结束日期" @change="getTime" value-format="yyyy-MM-dd" v-model="couponEndTime" type="date">
						</el-date-picker>
					</el-form-item>
				</el-col>
			</template>-->
		</avue-crud>
		<!-- 新增按钮弹框 -->
		<el-dialog title="新增优惠券" :visible.sync="dialogVisible" width="60%" :close-on-click-modal="false"  :before-close="closeDialog">
			<el-form :model="addForm" :rules="rules" ref="addForm" label-width="120px" class="demo-ruleForm">
				<el-form-item label="优惠券名称:" prop="couponName" style="width: 50%;display: inline-block;">
					<el-input :disabled="isView" v-model="addForm.couponName" placeholder="请输入优惠券名称"></el-input>
				</el-form-item>
				<el-form-item label="优惠券数量:" style="width: 50%;display: inline-block;">
					<el-tooltip class="item" effect="dark" content="数量为空或0时表示不限制数量" placement="top-start">
						<el-input-number :min='min' :max='max' v-model="addForm.couponCount"  placeholder="0"></el-input-number>
					</el-tooltip>
				</el-form-item>
				<el-form-item label="优惠券金额:" prop="couponAmount" style="width: 50%;display: inline-block; height: 50px">
					<el-input v-model="addForm.couponAmount" @blur="checkCouponPrice" placeholder="0"></el-input>
				</el-form-item>
				<el-form-item label="可用订单金额:" prop="couponOrderAmount" style="width: 50%;display: inline-block;">
					<el-tooltip class="item" effect="dark" content="订单金额达到该数值才可使用此优惠券(金额为0默认为代金券)" placement="top-start">
						<el-input v-model="addForm.couponOrderAmount" @blur="checkOrderPrice" placeholder="0"></el-input>
					</el-tooltip>
				</el-form-item>
				<el-form-item label="开始使用时间:" prop="couponBeginTime" style="width: 50%;display: inline-block;">
					<el-date-picker :readonly="isView" :picker-options="pickerOptions" v-model="addForm.couponBeginTime" value-format="yyyy-MM-dd" type="date"
					 placeholder="请选择开始使用时间" style="width: 100%;" @change="checkStartTime"></el-date-picker>
					<!-- <el-date-picker v-model="addForm.dateScope" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%;"></el-date-picker> -->
				</el-form-item>
				<el-form-item label="结束使用时间:" prop="couponEndTime" style="width: 50%;display: inline-block;">
					<el-date-picker :readonly="isView" @change="getAddTime" v-model="addForm.couponEndTime" value-format="yyyy-MM-dd"
					 type="date" placeholder="请选择结束使用时间" :picker-options="pickerOptions" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item label="优惠券描述:" prop="couponDesc">
					<el-input :disabled="isView" maxlength="50" show-word-limit type="textarea" v-model="addForm.couponDesc"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button v-if="!isView" @click="closeDialog">取 消</el-button>
				<el-button v-if="!isView" type="primary" @click="addRecurseData" :loading="addLoad">保 存</el-button>
				<el-button v-else @click="closeDialog">关闭</el-button>
			</span>
		</el-dialog>
		<!--活动商品-->
		<el-drawer title="活动商品" :visible.sync="activity" :with-header="true" size="60%">
			<div style="padding: 20px;">
				<avue-crud :data="activityData" :option="activityOption" :page="activityPage" @size-change="sizeChangeA"
				 @current-change="currentChangeA">
				</avue-crud>
			</div>
		</el-drawer>
		<!--优惠券详情-->
		<el-drawer title="优惠券详情" :visible.sync="coupon" :with-header="true" size="80%">
			<div style="padding: 20px;">
				<avue-crud :data="couponData" :option="couponOption" :page="couponPage" @size-change="sizeCouponChange"
				 @current-change="currentCouponChange">
				</avue-crud>
			</div>
		</el-drawer>
		<!--商品参加优惠-->
		<el-drawer title="参加优惠详情" :visible.sync="joinCoupon" size="80%" @close="closeJoin">
			<div class="join-detail">
				<el-col>
					<p class="coupon-name">优惠券名称：{{couponD.couponName}}</p>
				</el-col>
				<el-col>
					<p class="coupon-name">优惠券描述：{{couponD.couponDesc}}</p>
				</el-col>
				<!--				搜索框-->
				<el-form :inline="true" :model="formInline" class="demo-form-inline">
					<el-form-item label="商品名称">
						<el-input v-model="formInline.goodsName" placeholder="商品名称" @keyup.enter.native="onSubmit"></el-input>
					</el-form-item>
					<el-form-item label="指定分类">
						<el-select v-model="formInline.goodsClassId" placeholder="指定分类" clearable>
							<el-option v-for="item in selData" :key="item.id" :label="item.className" :value="item.id"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="供货商手机号">
						<el-input v-model="formInline.supplierMobile" placeholder="供货商手机号" @keyup.enter.native="onSubmit"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit">查询</el-button>
					</el-form-item>
				</el-form>
				<!--				选择输入框-->
				<div v-if="isCommodity">
					<avue-crud ref="crud" :data="commodityData" :option="commodityOption" :page="commodityPage" @selection-change="selectDataInfo"
					 @size-change="sizeChangecomm" @current-change="currentChangecomm">
						<template slot-scope="{row}" slot="goodsMainPhotoUrl">
							<div>
								<el-image style="width: 50px;height: 50px;" :src="row.goodsMainPhotoUrl"></el-image>
							</div>
						</template>
					</avue-crud>
				</div>
				<div class="submit-but">
					<el-button :disabled="isDisabled" type="primary" @click="addCommodity">提 交</el-button>
				</div>
			</div>
		</el-drawer>
	</basic-container>
</template>

<script>
	var Dict = JSON.parse(localStorage.getItem('mall-dictionary')).content;
	import {validatNum} from '@/util/validate.js'
	import {isInteger,testPirce,isInteger0,testPirceZero} from '@/util/util.js'
	var ISSUE_QUANTITY = [{
		label: '不限量',
		value: 0,
	}];
	import {
		getList,
		remove,
		update,
		add,
		listAllLevelThree,
		getGoods,
		setAddItem,
		getGoodsList,
		getCouponList,
		getListGoodsIds
	} from "@/api/base/store/seller/coupon";
	import {
		mapGetters
	} from "vuex";
	// import StoreTemplate from "../store/storeTemplate";
	export default {
		components: {
			// StoreTemplate
		},
		filters: {
			status(status) {
				if (status == 0) {
					return '未使用'
				} else if (status == 1) {
					return '已使用'
				}
			}
		},
		data() {
			return {
				pickerOptions: {
					disabledDate(time) {
						return time.getTime() + 3600 * 1000 * 24 * 1 < Date.now();
					},
				},
				max:100000000,
				min:0,
				isDisabled: false,
				addLoad: false,
				olaArr: [],
				activityDataRow: {},
				selChangeData: [],
				idList: [],
				selData: [],
				isCommodity: false,
				couponD: {},
				commodityPage: {
					pageSize: 20,
					total: 0,
					currentPage: 1
				},
				commodityData: [],
				commodityOption: {
					selection: true,
					selectable: (row) => {
						if(row.isJoinCardAlready) {
							return false;
						}else {
							return true;
						}
					},
					addBtn: false,
					cellBtn: false,
					delBtn: false,
					menu: false,
					align: 'center',
					border: true,
					column: [{
						label: '商品图片',
						prop: 'goodsMainPhotoUrl',
						width: 150,
						slot: true
					}, {
						label: '商品名称',
						prop: 'goodsName'
					}, {
						label: '总库存',
						prop: 'goodsStock',
						width: 150,
					}, {
						label: '商城售价(￥)',
						prop: 'showPrice',
						width: 150,
					}]
				},
				formInline: {
					goodsName: '',
					goodsClassId: '',
					supplierMobile: ''
				},
				joinCoupon: false,
				//优惠卷详情
				coupon: false,
				pageSize: 10,
				pageCurrent: 1,
				total: 0,
				tableDataRow: {},
				activityPage: {
					pageSize: 20,
					total: 0,
					currentPage: 1
				},
				activityData: [],
				couponData: [],
				couponQuery: {},
				couponPage: {
					pageSize: 20,
					total: 0,
					currentPage: 1
				},
				couponOption: {
					addBtn: false,
					align: 'center',
					menuAlign: 'center',
					menu: false,
					delBtn: false,
					editBtn: false,
					border: true,
					column: [{
							label: '优惠券名称',
							 align: 'center',
							prop: 'couponName'
						},
						{
							label: '用户昵称',
							width: 200,
							prop: 'userName'
						},
						{
							label: '手机号码',
							width: 150,
							prop: 'moblie'
						},
						{
							label: '优惠券金额',
							width: 150,
							prop: 'couponAmount'
						},
						{
							label: '使用时间',
							type: 'date',
							width: 200,
							prop: 'useTime'
						},
						{
							label: '是否使用',
							width: 100,
							dicData:Dict.COUPON_INFO_STATUS,
							prop: 'status'
						}
					]
				},
				activityOption: {
					addBtn: false,
					align: 'center',
					menuAlign: 'center',
					menu: false,
					delBtn: false,
					editBtn: false,
					border: true,
					column: [{
							label: '商品名称',
							prop: 'goodsName'
						},
						{
							label: '优惠券名称',
							width: 200,
							prop: 'couponName'
						},
						{
							type: 'date',
							label: '开始时间',
							width: 150,
							prop: 'couponBeginTime'
						},
						{
							type: 'date',
							label: '结束时间',
							width: 150,
							prop: 'couponEndTime'
						}
					]
				},
				//活动
				activity: false,
				couponEndTime: "",
				couponBeginTime: "",
				isView: false,
				//新增按钮弹框内容
				addForm: {
					couponName: "",
					// dateScope: "",
					couponEndTime: "",
					couponBeginTime: "",
					couponCount: "0", // 优惠券数量
					couponAmount: "",
					couponOrderAmount: "",
					couponGetType: "",
					couponStatus: "",
				},
				rules: {
					couponName: [{
							required: true,
							message: "优惠券名称不能为空",
							trigger: "blur"
						},
						{
							min: 2,
							max: 18,
							message: "长度在 2 到 18 个字符",
							trigger: "blur"
						}
					],
					couponBeginTime: [{
						type: 'date',
						required: true,
						message: "请选择开始使用时间",
						trigger: "blur"
					}, ],
					couponEndTime: [{
						type: 'date',
						required: true,
						message: "请选择结束使用时间",
						trigger: "blur"
					}],
					couponCount: [{
						required: true,
						message: "请设定优惠券数量",
						trigger: "blur",
					}, {
						validator: isInteger0,
						trigger: "blur",
						}
					],
					couponAmount: [{
						required: true,
						message: "请设定优惠券金额",
						trigger: "blur"
					}, {
						validator: testPirce,
						trigger: "blur",
					}],
					couponOrderAmount: [{
						required: true,
						message: "请填写可用订单金额",
						trigger: "blur"
					},
						{
						validator: testPirceZero,
						trigger: "blur",
					}
					],
					couponGetType: [{
						required: true,
						message: "请选择领取方式",
						trigger: "blur"
					},],
					couponDesc: [
						{
							min: 0,
							max: 50,
							message: "请输入50字以内的优惠券描述",
							trigger: "change"
						},
					],
				},
				dialogVisible: false,
				form: {},
				query: {
					couponType: 1
				},

				loading: true,
				rolesTree: [],
				rolesKeys: [],
				box: false,
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					excelBtn: false,
					addBtn: false,
					searchBtn: true,
					cancelBtn: false,
					tip: false,
					index: false,
					border: true,
					selection: false,
					viewBtn: false,
					delBtn: false,
					editBtn: false,
					rowKey: 'id',
					// calcHeight: 380,
					align: 'center',
					dialogType: 'dialog',
					menuWidth: 300,
					searchMenuSpan:6,
					column: [{
							label: "优惠劵名称",
							searchLabelWidth: 100,
							prop: "couponName",
							fixed:true,
							search: true,
						},
						{
							label: "优惠金额",
							prop: "couponAmount",
							width: 100,
						},
						{
							label: "订单金额",
							prop: "couponOrderAmount",
							width: 100,
						},
						{
							label: "使用时间",
							prop: "dateScope",
							startPlaceholder: '日期开始范围自定义',
							endPlaceholder: '日期结束范围自定义',
							width: 200,
							slot: true,
							formslot: true,
						},
						{
							label: "发行数量",
							prop: "couponCount",
							width: 100,
							dicData: ISSUE_QUANTITY,
						},
						{
							label: "状态",
							prop: "couponStatus",
							width: 100,
							type: 'switch',
							slot: true,
							cell: true,
							// diData: Dict.COUPON_STATUS,
						},
					]
				},
				data: [],
				queryCommodity: {},
				// dataStore: [],
			};
		},
		computed: {
			...mapGetters(["permission", 'userInfo']),
			permissionList() {
				return {

				};
			},
		},
		watch: {
			'addForm.couponCount'(val) {
				console.log('val',val)
				if(val) {
					if(parseInt(val)==100000000 && val!=undefined || !validatNum(val) && val!=undefined) {
						this.$message({
							message: '优惠券数量为0-100000000之间的整数,已为您设置最大优惠券数量',
							type: 'warning'
						})
					}
				}
			}
		},
		//tabs切换
		created() {
			this.type = this.option.column[0];
		},
		methods: {
			sizeChangecomm(val) {
				this.commodityPage.pageSize = val
				this.getG()
			},
			currentChangecomm(val) {
				this.commodityPage.currentPage = val
				this.getG()

			},
			// 优惠券字数校验函数
			// checkoutInput(value) {
			// 	if(value.toString().length>50) {
			// 		this.$message({
			// 			showClose: true,
			// 			message: '优惠券描述不可超过50字',
			// 			type: 'warning'
			// 		});
			// 	}
			// },
			//查看商品详情
			commodityDetails(row) {
				console.log(row)
			},
			//关闭
			closeJoin() {
				this.isCommodity = false
				this.formInline = {
					goodsName: '',
					goodsClassId: '',
					supplierMobile: ''
				};
			},
			//启用或暂停
			rowSwitchChange(row) {
				console.log(row);
				if (row.couponStatus == 2) {
					update(row).then(res => {
						console.log(res)
						if (res.status == 200) {
							this.$message({
								type: "success",
								message: "关闭成功!"
							});
						}
					})
				} else if (row.couponStatus == 1) {
					update(row).then(res => {
						console.log(res)
						if (res.status == 200) {
							this.$message({
								type: "success",
								message: "开启成功!"
							});
						}
					})
				}
			},
			//保存参与优惠的商品
			addCommodity() {
				if (this.idList.length != 0) {
					//	发送要优惠商品的id数组
					setAddItem(this.couponD.id, this.idList).then(res => {
						if (res.status == 200) {
							this.joinCoupon = false
							this.$message({
								message: '保存成功',
								type: 'success'
							})
						}
					})
					this.getG();
					this.isDisabled=true
				} else {
					this.$message({
						message: '请选择商品',
						type: 'error'
					})
				}
			},
			selectDataInfo(list) {
				this.idList = [];
				list.forEach((item, index) => {
					this.idList[index] = item.id;
				});
				console.log(this.idList);
			},

			//获取商品的数据
			getG() {
				this.queryCommodity = this.formInline;
				//	获取参与的商品
				getListGoodsIds(this.couponD.id).then(res => {
					console.log(res);
					if (res.status == 200) {
						this.selChangeData = res.data.data;
					}
				})
				getGoods(this.commodityPage.currentPage, this.commodityPage.pageSize, this.queryCommodity).then(res => {
					if (res.status == 200) {
						this.commodityData = res.data.data;
						this.commodityPage.total = res.data.count;
						for (let i = 0; i < this.commodityData.length; i++) {
							if (this.selChangeData.indexOf(this.commodityData[i].id) > -1) {
								this.commodityData[i].isJoinCardAlready = true;
							}
							else {
								this.commodityData[i].isJoinCardAlready = false;
							}
						}
						console.log(this.commodityData);
					}
				})
			},
			//点击搜索
			onSubmit() {
				this.commodityPage.currentPage = 1;
				this.isCommodity = true
				this.getG()
			},
			handleSizeChange(val) {
				this.pageSize = val
				this.getTalRow()
			},
			handleCurrentChange(val) {
				this.pageCurrent = val
				this.getTalRow()
			},
			//发放优惠券
			grantShow(row) {
				console.log(row)
				this.joinCoupon = true
				this.couponD = row
				//	获取下拉列表
				listAllLevelThree().then(res => {
					if (res.status == 200) {
						this.selData = res.data.data
					}
				})
				this.commodityPage.currentPage = 1;
				this.isCommodity = true;
				this.getG();
				this.isDisabled=false;
			},
			//获取活动商品
			getGoods() {
				let obj = {
					couponId: this.activityDataRow.id
				}
				getGoodsList(this.activityPage.currentPage, this.activityPage.pageSize, obj).then(res => {
					if (res.status == 200) {
						for (let item of res.data.data) {
							item.couponName = this.activityDataRow.couponName
							item.couponBeginTime = this.activityDataRow.couponBeginTime
							item.couponEndTime = this.activityDataRow.couponEndTime
						}
						this.activityData = res.data.data
						this.activityPage.total = res.data.count
					}
				})

			},
			//活动商品
			goodsDetailShow(row) {
				this.activityDataRow = row
				this.activity = true
				this.getGoods()
			},

			//点击页数
			sizeChangeA(val) {
				this.activityPage.pageSize = val
				this.getGoods()
			},
			currentChangeA(val) {
				this.activityPage.currentPage = val
				this.getGoods()
			},
			//搜索时间范围限制
			getTime() {
				if (this.couponBeginTime > this.couponEndTime) {
					this.$message({
						type: "error",
						message: "结束时间不能小于开始时间"
					});
					this.couponEndTime = ''
				}
			},
			//校验开始时间
			checkStartTime () {
				if (this.addForm.couponEndTime) {
					if (new Date(this.addForm.couponBeginTime) > new Date(this.addForm.couponEndTime)) {
						this.$message({
							type: "error",
							message: "开始时间不能大于结束时间"
						});
						this.addForm.couponBeginTime = '';
						return;
					}
				}
			},
			//新增弹框 优惠券使用范围现在
			getAddTime() {
				if (new Date(this.addForm.couponBeginTime) > new Date(this.addForm.couponEndTime) && this.addForm.couponBeginTime && this.addForm.couponEndTime) {
					this.$message({
						type: "error",
						message: "结束时间不能小于开始时间"
					});
					this.addForm.couponEndTime = ''
				}
			},
			//点击页数
			sizeCouponChange(val) {
				this.couponPage.pageSize = val
			},
			currentCouponChange(val) {
				this.couponPage.currentPage = val
			},
			getTalRow(page, params = {}) {
				getCouponList(page.currentPage, page.pageSize, Object.assign(params, this.couponQuery)).then(res => {
					if (res.status == 200) {
						this.couponData = res.data.data
						this.couponPage.total = res.data.count
					}
				})

			},
			//查看优惠券详情
			detailShow(row) {
				console.log(row)
				this.couponQuery.couponId = row.id
				this.coupon = true
				this.getTalRow(this.couponPage)
			},
			// 新增或者编辑 弹框 弹窗保存发放
			addRecurseData() {
				this.addLoad = true;
				if (this.addForm.couponName) {
					if (this.addForm.couponName.length > 18 || this.addForm.couponName.length < 2) {

						this.$message({
							type: "error",
							message: "优惠劵名称长度在 2 到 18 个字符!"
						});
						this.addLoad = false;
						return;
					}
				}
				if (!validatNum(this.addForm.couponCount) && this.addForm.couponCount ||this.addForm.couponCount>100000000 && this.addForm.couponCount) {
					this.$message({
						type: "error",
						message: "优惠券数量为0-100000000之间的整数"
					});
					this.addLoad = false;
					return;
				}
				var couponCountFlag = true;
				if (!isNaN(this.addForm.couponCount || !this.addForm.couponCount)) {
					if(!this.addForm.couponCount){
						this.addForm.couponCount = 0;
					}
					couponCountFlag = false;
				}

				var orderAmountFlag = true;
				if (!isNaN(this.addForm.couponOrderAmount || !this.addForm.couponOrderAmount)) {
					if (!this.addForm.couponOrderAmount) {
						this.addForm.couponOrderAmount = 0;
					}
					orderAmountFlag = false;
				}
				if(!isNaN(this.addForm.couponAmount || !this.addForm.couponAmount)){
					if(!this.addForm.couponAmount){
						this.$message({
							type: "error",
							message: "优惠券金额应大于0"
						});
						this.addLoad = false;
						return
					}
				}
				if(this.addForm.couponAmount >10000){
					this.$message({
						type: "error",
						message: "优惠券金额不能大于10000"
					});
					this.addLoad = false;
					return;
				}
				if (!this.addForm.couponName || couponCountFlag  || !this.addForm.couponBeginTime ||
					!this.addForm.couponEndTime || orderAmountFlag
				) {
					this.$message({
						type: "error",
						message: "请填写优惠券必要信息!"
					});
					this.addLoad = false;
					return
				}
				add(this.addForm).then(res => {
					if (res.data.code == 200) {
						//前台日期数据格式转化为后台可接收数据格式
						let data = res.data.data
						this.form = data
						console.log(this.form)
						this.$message({
							type: "success",
							message: "新增优惠券成功!"
						});
						this.onLoad(this.page);
						this.addForm = {};
						this.dialogVisible = false
					} else {
						this.$message({
							type: "error",
							message: res.data.message
						});
					}
					this.addLoad = false;
				})

			},
			// 新增关闭按钮
			closeDialog() {
				this.dialogVisible = false;
				this.$refs.addForm.resetFields();
			},

			// 点击编辑按钮，调用根据id查询信息按钮，获取信息显示在页面上
			editShow(row) {
				this.isView = false
				this.addForm = row
				this.dialogVisible = true;
			},
			// 点击新增优惠券
			addCoupon() {
				this.isView = false
				this.addForm = {}
				// this.$refs.addForm.resetFields();
				this.dialogVisible = true;
			},
			rowDel(row) {
				this.$confirm("确定将选择数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return remove(row.id);
					})
					.then(() => {
						this.onLoad(this.page);
						this.$message({
							type: "success",
							message: "操作成功!"
						});
					});
			},
			searchReset() {
				this.query = {};
				this.onLoad(this.page);
			},
			searchChange(params,done) {
				this.page.currentPage = 1
				this.query.couponName = params.couponName
				this.onLoad(this.page, this.query,done);
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			refreshChange() {
				this.onLoad(this.page);
			},
			onLoad(page, params = {},done) {
				this.loading = true;
				getList(
					page.currentPage,
					page.pageSize,
					Object.assign(params, this.query)
				).then(res => {
					if(done){
						done();
					}
					this.loading = false;
					const data = res.data;
					this.page.total = data.count;
					for (let item of data.data) {
						item.couponBeginTime = item.couponBeginTime.substring(0, 10)
						item.couponEndTime = item.couponEndTime.substring(0, 10)
						item.couponStatus = item.couponStatus.toString();
					}
					this.data = data.data;
				});
			},
			//校验优惠券金额
			checkCouponPrice() {
				if (this.addForm.couponOrderAmount && this.addForm.couponAmount) {
					this.addForm.couponAmount = Number(this.addForm.couponAmount);
					this.addForm.couponOrderAmount = Number(this.addForm.couponOrderAmount);
					if (this.addForm.couponAmount > this.addForm.couponOrderAmount) {
						this.$message({
							type: "error",
							message: "优惠券金额不能大于订单可用金额"
						});
						this.addForm.couponAmount = "";
						return;
					}
				}
				if(this.addForm.couponAmount == 0){
					this.$message({
						type: "error",
						message: "优惠券金额需要大于0"
					});
					this.addForm.couponAmount = "";
					return;
				}
				if(this.addForm.couponAmount >10000){
					this.$message({
						type: "error",
						message: "优惠券金额不能大于10000"
					});
					this.addForm.couponAmount = "";
					return;
				}
			},
			//校验订单可用金额
			checkOrderPrice() {
				if (this.addForm.couponOrderAmount && this.addForm.couponAmount) {
					this.addForm.couponAmount = Number(this.addForm.couponAmount);
					this.addForm.couponOrderAmount = Number(this.addForm.couponOrderAmount);
					if ((this.addForm.couponAmount > this.addForm.couponOrderAmount) && (this.addForm.couponOrderAmount != 0)) {
						this.$message({
							type: "error",
							message: "优惠券金额不能大于订单可用金额"
						});
						this.addForm.couponOrderAmount = "";
						return;
					}
				}
			},
		}
	};
</script>

<style scoped>
	@media only screen and (min-width: 992px) {}

	.el-col-md-6 {
		width: 30% !important;
	}

	.join-detail {
		padding: 20px;
	}

	.coupon-name {
		line-height: 30px;
		font-size: 15px;
	}

	.submit-but {
		padding: 20px;
		text-align: right;
	}
</style>
